<template>
  <div class="login-page">
    <van-nav-bar class="page-nav-bar" title="账号登录" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        center
        :rules="[
        {required: true},
       { pattern: /^[a-zA-Z]{5,}$/, message: '用户名至少包含5位的字符'}
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[ 
          {required: true},
         { pattern: /^\d{5,12}$/, message: '密码格式5-12位的字符和数字'}
         ]"
      />
      <div style="margin: 16px;">
        <van-button square block type="info" native-type="submit">登录</van-button>
      </div>
      <div class="register">还没有账号，去注册~</div>
    </van-form>
  </div>
</template>

<script>
 import {setItem,key} from '@/utils/storage'
 import { loginApi } from '@/api/user'

export default {
  props: {},
  data() {
    return {
      username: '',
      password: '',
    }
  },
  computed: {},
  created() {},
  mounted() {},
  watch: {},
  methods: {
    onClickLeft() {
      // console.log(1)
      this.$router.push({ path: '/layout/my' })
    },
    async onSubmit(values) {
      // console.log(values);
      try {
       const {body:{token}}= await loginApi(values)
      // console.log(token);
       setItem(key,token)
       this.$toast.success('登录成功');
       this.$router.push('/layout/my')
      } catch (err) {
        //console.dir(err);
      }
      
    },
  },
  components: {},
}
</script>

<style scoped lang="less">
.page-nav-bar {
  background-color: #21b97a;
  //  font-size: 18px;
  //  height:45px
}

.van-button--normal {
  font-size: 20px;
  background-color: #21b97a;
}
// .van-cell {
//   height: 40px;
//   line-height: 40px;
//   padding-top: 2px;
//   padding-bottom: 2px;
//   font-size: 17px;
// }
.login-page .register {
  color: #666;
  font-size: 14px;
  text-align: center;
  margin-top: 10px;
}
</style>
